home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / srcuc.zip / TPARAM.C < prev    next >
C/C++ Source or Header  |  1992-02-10  |  12KB  |  371 lines

  1. /* Merge parameters into a termcap entry string.
  2.    Copyright (C) 1985-1992 Free Software Foundation, Inc.
  3.  
  4.  
  5.                NO WARRANTY
  6.  
  7.   BECAUSE THIS PROGRAM IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY
  8. NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW.  EXCEPT
  9. WHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC,
  10. RICHARD M. STALLMAN AND/OR OTHER PARTIES PROVIDE THIS PROGRAM "AS IS"
  11. WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
  12. BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  13. FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY
  14. AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE PROGRAM PROVE
  15. DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
  16. CORRECTION.
  17.  
  18.  IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.
  19. STALLMAN, THE FREE SOFTWARE FOUNDATION, INC., AND/OR ANY OTHER PARTY
  20. WHO MAY MODIFY AND REDISTRIBUTE THIS PROGRAM AS PERMITTED BELOW, BE
  21. LIABLE TO YOU FOR DAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR
  22. OTHER SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
  23. USE OR INABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR
  24. DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR
  25. A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) THIS
  26. PROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
  27. DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
  28.  
  29.         GENERAL PUBLIC LICENSE TO COPY
  30.  
  31.   1. You may copy and distribute verbatim copies of this source file
  32. as you receive it, in any medium, provided that you conspicuously and
  33. appropriately publish on each copy a valid copyright notice "Copyright
  34. (C) 1986 Free Software Foundation, Inc."; and include following the
  35. copyright notice a verbatim copy of the above disclaimer of warranty
  36. and of this License.  You may charge a distribution fee for the
  37. physical act of transferring a copy.
  38.  
  39.   2. You may modify your copy or copies of this source file or
  40. any portion of it, and copy and distribute such modifications under
  41. the terms of Paragraph 1 above, provided that you also do the following:
  42.  
  43.     a) cause the modified files to carry prominent notices stating
  44.     that you changed the files and the date of any change; and
  45.  
  46.     b) cause the whole of any work that you distribute or publish,
  47.     that in whole or in part contains or is a derivative of this
  48.     program or any part thereof, to be licensed at no charge to all
  49.     third parties on terms identical to those contained in this
  50.     License Agreement (except that you may choose to grant more extensive
  51.     warranty protection to some or all third parties, at your option).
  52.  
  53.     c) You may charge a distribution fee for the physical act of
  54.     transferring a copy, and you may at your option offer warranty
  55.     protection in exchange for a fee.
  56.  
  57. Mere aggregation of another unrelated program with this program (or its
  58. derivative) on a volume of a storage or distribution medium does not bring
  59. the other program under the scope of these terms.
  60.  
  61.   3. You may copy and distribute this program (or a portion or derivative
  62. of it, under Paragraph 2) in object code or executable form under the terms
  63. of Paragraphs 1 and 2 above provided that you also do one of the following:
  64.  
  65.     a) accompany it with the complete corresponding machine-readable
  66.     source code, which must be distributed under the terms of
  67.     Paragraphs 1 and 2 above; or,
  68.  
  69.     b) accompany it with a written offer, valid for at least three
  70.     years, to give any third party free (except for a nominal
  71.     shipping charge) a complete machine-readable copy of the
  72.     corresponding source code, to be distributed under the terms of
  73.     Paragraphs 1 and 2 above; or,
  74.  
  75.     c) accompany it with the information you received as to where the
  76.     corresponding source code may be obtained.  (This alternative is
  77.     allowed only for noncommercial distribution and only if you
  78.     received the program in object code or executable form alone.)
  79.  
  80. For an executable file, complete source code means all the source code for
  81. all modules it contains; but, as a special exception, it need not include
  82. source code for modules which are standard libraries that accompany the
  83. operating system on which the executable file runs.
  84.  
  85.   4. You may not copy, sublicense, distribute or transfer this program
  86. except as expressly provided under this License Agreement.  Any attempt
  87. otherwise to copy, sublicense, distribute or transfer this program is void and
  88. your rights to use the program under this License agreement shall be
  89. automatically terminated.  However, parties who have received computer
  90. software programs from you with this License Agreement will not have
  91. their licenses terminated so long as such parties remain in full compliance.
  92.  
  93.   5. If you wish to incorporate parts of this program into other free
  94. programs whose distribution conditions are different, write to the Free
  95. Software Foundation at 675 Mass Ave, Cambridge, MA 02139.  We have not yet
  96. worked out a simple rule that can be stated here, but we will often permit
  97. this.  We will be guided by the two goals of preserving the free status of
  98. all derivatives of our free software and of promoting the sharing and reuse of
  99. software.
  100.  
  101.  
  102. In other words, you are welcome to use, share and improve this program.
  103. You are forbidden to forbid anyone else to use, share and improve
  104. what you give them.   Help stamp out software-hoarding!  */
  105.  
  106.  
  107. /* config.h may rename various library functions such as malloc.  */
  108. #ifdef emacs
  109. #include "config.h"
  110. #endif
  111.  
  112. #include "ansidecl.h"
  113.  
  114. #ifndef alpha
  115.   extern char * EXFUN (tparam, (char *, char*, int, int, ...));
  116. #else
  117.   extern char * tparam ();
  118. #endif
  119. extern char * EXFUN (tgoto, (char *, int, int));
  120.  
  121. /* Assuming STRING is the value of a termcap string entry
  122.    containing `%' constructs to expand parameters,
  123.    merge in parameter values and store result in block OUTSTRING points to.
  124.    LEN is the length of OUTSTRING.  If more space is needed,
  125.    a block is allocated with `malloc'.
  126.  
  127.    The value returned is the address of the resulting string.
  128.    This may be OUTSTRING or may be the address of a block got with `malloc'.
  129.    In the latter case, the caller must free the block.
  130.  
  131.    The fourth and following args to tparam serve as the parameter values.  */
  132.  
  133. char * EXFUN (tparam1, (char *, char *, int, char *, char *, int *));
  134.  
  135. /* VARARGS 2 */
  136. char *
  137. tparam (string, outstring, len, arg0, arg1, arg2, arg3)
  138.      char *string;
  139.      char *outstring;
  140.      int len;
  141.      int arg0, arg1, arg2, arg3;
  142. {
  143. #ifdef NO_ARG_ARRAY
  144.   int arg[4];
  145.   arg[0] = arg0;
  146.   arg[1] = arg1;
  147.   arg[2] = arg2;
  148.   arg[3] = arg3;
  149.   return tparam1 (string, outstring, len, 0, 0, arg);
  150. #else
  151.   return tparam1 (string, outstring, len, 0, 0, &arg0);
  152. #endif
  153. }
  154.  
  155. char *BC;
  156. char *UP;
  157.  
  158. static char tgoto_buf[50];
  159.  
  160. char *
  161. tgoto (cm, hpos, vpos)
  162.      char *cm;
  163.      int hpos, vpos;
  164. {
  165.   int args[2];
  166.   if (!cm)
  167.     return 0;
  168.   args[0] = vpos;
  169.   args[1] = hpos;
  170.   return tparam1 (cm, tgoto_buf, 50, UP, BC, args);
  171. }
  172.  
  173. static char *
  174. tparam1 (string, outstring, len, up, left, argp)
  175.      char *string;
  176.      char *outstring;
  177.      int len;
  178.      char *up, *left;
  179.      register int *argp;
  180. {
  181.   register int c;
  182.   register char *p = string;
  183.   register char *op = outstring;
  184.   char *outend;
  185.   int outlen = 0;
  186.  
  187.   register int tem;
  188.   int *oargp = argp;
  189.   int doleft = 0;
  190.   int doup = 0;
  191.  
  192.   outend = outstring + len;
  193.  
  194.   while (1)
  195.     {
  196.       /* If the buffer might be too short, make it bigger.  */
  197.       if (op + 5 >= outend)
  198.     {
  199.       register char *new;
  200.       if (outlen == 0)
  201.         {
  202.           new = (char *) malloc (outlen = 40 + len);
  203.           outend += 40;
  204.           bcopy (outstring, new, op - outstring);
  205.         }
  206.       else
  207.         {
  208.           outend += outlen;
  209.           new = (char *) realloc (outstring, outlen *= 2);
  210.         }
  211.       op += new - outstring;
  212.       outend += new - outstring;
  213.       outstring = new;
  214.     }
  215.       if (!(c = *p++))
  216.     break;
  217.       if (c == '%')
  218.     {
  219.       c = *p++;
  220.       tem = *argp;
  221.       switch (c)
  222.         {
  223.         case 'd':        /* %d means output in decimal */
  224.           if (tem < 10)
  225.         goto onedigit;
  226.           if (tem < 100)
  227.         goto twodigit;
  228.         case '3':        /* %3 means output in decimal, 3 digits. */
  229.           if (tem > 999)
  230.         {
  231.           *op++ = tem / 1000 + '0';
  232.           tem %= 1000;
  233.         }
  234.           *op++ = tem / 100 + '0';
  235.         case '2':        /* %2 means output in decimal, 2 digits. */
  236.         twodigit:
  237.           tem %= 100;
  238.           *op++ = tem / 10 + '0';
  239.         onedigit:
  240.           *op++ = tem % 10 + '0';
  241.           argp++;
  242.           break;
  243.  
  244.         case 'C':
  245.           /* For c-100: print quotient of value by 96, if nonzero,
  246.          then do like %+ */
  247.           if (tem >= 96)
  248.         {
  249.           *op++ = tem / 96;
  250.           tem %= 96;
  251.         }
  252.         case '+':        /* %+x means add character code of char x */
  253.           tem += *p++;
  254.         case '.':        /* %. means output as character */
  255.           if (left)
  256.         {
  257.           /* If want to forbid output of 0 and \n and \t,
  258.              and this is one of them, increment it.  */
  259.           while (tem == 0 || tem == '\n' || tem == '\t')
  260.             {
  261.               tem++;
  262.               if (argp == oargp)
  263.             doup++, outend -= strlen (up);
  264.               else
  265.             doleft++, outend -= strlen (left);
  266.             }
  267.         }
  268.           *op++ = tem | 0200;
  269.         case 'f':        /* %f means discard next arg */
  270.           argp++;
  271.           break;
  272.  
  273.         case 'b':        /* %b means back up one arg (and re-use it) */
  274.           argp--;
  275.           break;
  276.  
  277.         case 'r':        /* %r means interchange following two args */
  278.           argp[0] = argp[1];
  279.           argp[1] = tem;
  280.           oargp++;
  281.           break;
  282.  
  283.         case '>':        /* %>xy means if arg is > char code of x, */
  284.           if (argp[0] > *p++) /* then add char code of y to the arg, */
  285.         argp[0] += *p;    /* and in any case don't output. */
  286.           p++;        /* Leave the arg to be output later. */
  287.           break;
  288.  
  289.         case 'a':        /* %a means arithmetic */
  290.           /* Next character says what operation.
  291.          Add or subtract either a constant or some other arg */
  292.           /* First following character is + to add or - to subtract
  293.          or = to assign.  */
  294.           /* Next following char is 'p' and an arg spec
  295.          (0100 plus position of that arg relative to this one)
  296.          or 'c' and a constant stored in a character */
  297.           tem = p[2] & 0177;
  298.           if (p[1] == 'p')
  299.         tem = argp[tem - 0100];
  300.           if (p[0] == '-')
  301.         argp[0] -= tem;
  302.           else if (p[0] == '+')
  303.         argp[0] += tem;
  304.           else if (p[0] == '*')
  305.         argp[0] *= tem;
  306.           else if (p[0] == '/')
  307.         argp[0] /= tem;
  308.           else
  309.         argp[0] = tem;
  310.  
  311.           p += 3;
  312.           break;
  313.  
  314.         case 'i':        /* %i means add one to arg, */
  315.           argp[0] ++;    /* and leave it to be output later. */
  316.           argp[1] ++;    /* Increment the following arg, too!  */
  317.           break;
  318.  
  319.         case '%':        /* %% means output %; no arg. */
  320.           goto ordinary;
  321.  
  322.         case 'n':        /* %n means xor each of next two args with 140 */
  323.           argp[0] ^= 0140;
  324.           argp[1] ^= 0140;
  325.           break;
  326.  
  327.         case 'm':        /* %m means xor each of next two args with 177 */
  328.           argp[0] ^= 0177;
  329.           argp[1] ^= 0177;
  330.           break;
  331.  
  332.         case 'B':        /* %B means express arg as BCD char code. */
  333.           argp[0] += 6 * (tem / 10);
  334.           break;
  335.  
  336.         case 'D':        /* %D means weird Delta Data transformation */
  337.           argp[0] -= 2 * (tem % 16);
  338.           break;
  339.         }
  340.     }
  341.       else
  342.     /* Ordinary character in the argument string.  */
  343.       ordinary:
  344.     *op++ = c;
  345.     }
  346.   *op = 0;
  347.   while (doup-- > 0)
  348.     strcat (op, up);
  349.   while (doleft-- > 0)
  350.     strcat (op, left);
  351.   return outstring;
  352. }
  353.  
  354. #ifdef DEBUG
  355.  
  356. main (argc, argv)
  357.      int argc;
  358.      char **argv;
  359. {
  360.   char buf[50];
  361.   int args[3];
  362.   args[0] = atoi (argv[2]);
  363.   args[1] = atoi (argv[3]);
  364.   args[2] = atoi (argv[4]);
  365.   tparam1 (argv[1], buf, "LEFT", "UP", args);
  366.   printf ("%s\n", buf);
  367.   return 0;
  368. }
  369.  
  370. #endif /* DEBUG */
  371.